import type { App } from "vue";
import Dialog, { FormData } from "./index.vue";

export const dialogFunc = (): Promise<FormData> => {
  return new Promise((resolve, reject) => {
    const mountNode = document.createElement("div");
    let dialogApp: App<Element> = createApp(Dialog, {
      visible: true,
      title: "校验",
      close: () => {
        if (dialogApp) {
          dialogApp.unmount();
          document.body.removeChild(mountNode);
          dialogApp = undefined;
          reject("close");
        }
      },
      confirm: (data: FormData) => {
        resolve(data);
        dialogApp.unmount();
        document.body.removeChild(mountNode);
        dialogApp = undefined;
      },
    });
    document.body.appendChild(mountNode);
    dialogApp.mount(mountNode);
  });
};
